﻿#define _CRT_SECURE_NO_WARNINGS
#include "SList.h"
void SListPrint(SLTNode*phead)
{
	SLTNode* cur = phead;
	while (cur!= NULL)
	{
		printf("%d->",cur->data);
		cur = cur->next;
	}
	printf("NULL\n");
}
SLTNode* BuySListNode(SLTDataType x)
{
	SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
	newnode->data = x;
	newnode->next = NULL;
	return newnode;
}
void SListPushBack(SLTNode**pphead,SLTDataType x)
{
	SLTNode* newnode = BuySListNode(x);
	if(*pphead==NULL)//空
		{
		*pphead = newnode;
		}
	else//一个及以上节点
	{
		SLTNode* tail = *pphead;
		while (tail->next!=NULL)
		{
			tail = tail->next;
		}
		tail->next = newnode;
	}
}
void SListPushFront(SLTNode**pphead,SLTDataType x)
{
	SLTNode* newnote = BuySListNode(x);
	newnote->next = *pphead;
	*pphead = newnote;
}
void SListPopBack(SLTNode**pphead)
{
	//空
	if (*pphead == NULL)
	{
		return;
	}
	//一个节点
	else if ((*pphead)->next==NULL)
	{
		free(*pphead);
		*pphead = NULL;
	}
	else//一个以上
	{
		SLTNode* prev = NULL;
		SLTNode* tail = *pphead;
		while (tail->next != NULL)
		{
			prev = tail;
			tail = tail->next;
		}
		prev->next = NULL;
		free(tail);
	}
}
void SListPopFront(SLTNode**pphead)
{
	SLTNode* next = (*pphead)->next;
	free(*pphead);
	*pphead = next;
}
SLTNode* SListFind(SLTNode*phead,SLTDataType x)
{
	SLTNode* cur = phead;
	while (cur)
	{
		if (cur->data == x)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}
void SListInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x)
{
	if (pos == *pphead)
	{
		SListPushBack(*pphead, x);
	}
	else
	{
		SLTNode* newnote = BuySListNode(x);
		SLTNode* prve = *pphead;
		while (prve->next != pos)
		{
			prve = prve->next;
		}
		prve->next = newnote;
		newnote->next = pos;
	}
}
void SListErase(SLTNode**pphead,SLTNode* pos)
{
	if (*pphead == pos)
	{
		SListPopFront(pphead);
	}
	else
	{
		SLTNode* prve = *pphead;
		while (prve->next!=pos)
		{
			prve = prve->next;
		}
		prve->next = pos->next;
		free(pos);
	}
}